DEFINITION MODULE AES;

(*
Global AES Definitions.

UK __DATE__ __TIME__
*)

(*DEF_SWITCHES*)

FROM PORTAB IMPORT ANYBYTE,ANYWORD,ANYLONG,SIGNEDWORD,UNSIGNEDWORD,
                   UNSIGNEDLONG,SIGNEDBYTE,UNSIGNEDBYTE,
                   BYTESET,WORDSET,ANYPOINTER;

IMPORT RcMgr;

#if export
EXPORT QUALIFIED GPnt,GRect,
                 ScanWord,Key,SpecialKeys,SpecialKey,
                 MouseButtons,MouseButton,
                 Root,MaxObject,ObjectIndex,ObjectTypes,
                 ObjectFlags,ObjectFlag,None,
                 ObjectStates,ObjectState,Normal,
                 IBM,Small,TextAlignments,ObjectColors,
                 DWhite,DBlack,DRed,DGreen,DBlue,DCyan,DYellow,DMagenta,
                 ObjectField,PatternShift,PatternOffset,InsidePatterns,
                 ModeShift,ModeOffset,InsideModes,TextShift,TextOffset,
                 FrameShift,FrameOffset,
                 MaxLen,StringRange,String,StringPtr,TEdInfo,TEdInfoPtr,
                 IconColor,MaskShift,DataShift,IconBlk,IconBlkPtr,
                 CIconPtr,CIcon,CIconBlk,CIconBlkPtr,
                 BitBlk,BitBlkPtr,
                 UserBlkPtr,ApplBlkPtr,ExtensionPtr,ObjectSpec,
                 ObjectExtension,
                 Nil,ObjectPtr,Object,TreePtr,
                 UserDefParm,ParmBlk,UserDefFunc,UserDefCode,UserDefBlk,
                 UserBlk,ApplBlk,
                 ControlArray,Control,
                 MaxTree,TreeIndex,TrPtr,RsHdr,RsPtr,
                 GlobalBlk,Global,
                 Integer,IntegerInput,IntegerOutput,IntIn,IntOut,
                 Address,AddressArray,Addr,
                 AESPB,c,crystal,
                 Error,
#if ST
                 KAOS,
#endif
                 Version;
#endif

(* point (AES.GPnt) *)

TYPE GPnt = RcMgr.GPnt;

(* rectangle (AES.GRect) *)

TYPE GRect = RcMgr.GRect;

(* key (AES.Key) *)

TYPE ScanWord = SIGNEDWORD;

#if packing
#if linear
     Key = RECORD
             CASE: BOOLEAN OF
               TRUE:
                 ScanCode: ScanWord;
             | FALSE:
                 Scan: UNSIGNEDBYTE;
                 Char: CHAR;
             END;
           END;
#elif intel
TYPE Key = RECORD
             CASE: BOOLEAN OF
               TRUE:
                 ScanCode: ScanWord;
             | FALSE:
                 Char: CHAR;
                 Scan: UNSIGNEDBYTE;
             END;
           END;
#endif
#else
TYPE Key = ScanWord;
#endif

(* special keys *)

#if revers
TYPE SpecialKeys = (K15,K14,K13,K12,K11,K10,K9,K8,K7,K6,K5,K4,
                    KAlt,
                    KCtrl,
                    KLShift,
                    KRShift);
#else
TYPE SpecialKeys = (KRShift,
                    KLShift,
                    KCtrl,
                    KAlt,
                    K4,K5,K6,K7,K8,K9,K10,K11,K12,K13,K14,K15);
#endif
     SpecialKey = PACKEDSET OF SpecialKeys;

(* mouse buttons *)

#if revers
TYPE MouseButtons = (Button16,Button15,Button14,Button13,Button12,Button11,
                     Button10,Button9,Button8,Button7,Button6,Button5,
                     Button4,Button3,
                     MBRight,
                     MBLeft);
#else
TYPE MouseButtons = (MBLeft,
                     MBRight,
                     Button3,Button4,Button5,Button6,Button7,Button8,
                     Button9,Button10,Button11,Button12,Button13,
                     Button14,Button15,Button16);
#endif
     MouseButton = PACKEDSET OF MouseButtons;

(* object index *)

CONST Root      =   0;
      MaxObject = 512; (* arbitrary limit *)

TYPE ObjectIndex = [Root..(MaxObject - 1)];

(* object types *)

TYPE ObjectTypes = (GObjc0,GObjc1,GObjc2,GObjc3,GObjc4,GObjc5,GObjc6,GObjc7,
                    GObjc8,GObjc9,GObjc10,GObjc11,GObjc12,GObjc13,GObjc14,
                    GObjc15,GObjc16,GObjc17,GObjc18,GObjc19,
                    GBox,
                    GText,
                    GBoxText,
                    GImage,
                    GUserDef,
                    GIBox,
                    GButton,
                    GBoxChar,
                    GString,
                    GFText,
                    GFBoxText,
                    GIcon,
                    GTitle,
                    GCIcon,
                    GSwButton, (* MagX *)
                    GPopup,    (* MagX *)
                    GObjc36,GObjc37,GObjc38,GObjc39);

(* object flags *)

#if revers
TYPE ObjectFlags = (Flag15,Flag14,Flag13,Flag12,
                    Submenu,
                    Fl3DBak,
                    Fl3DInd,
                    Indirect,
                    HideTree,
                    TouchExit,
                    LastOb,    (* [Last] [Ob]ject  *)
                    RButton,   (* [R]adio [Button] *)
                    Editable,
                    Exit,
                    Default,
                    Selectable);
#else
TYPE ObjectFlags = (Selectable,
                    Default,
                    Exit,
                    Editable,
                    RButton,   (* [R]adio [Button] *)
                    LastOb,    (* [Last] [Ob]ject  *)
                    TouchExit,
                    HideTree,
                    Indirect,
                    Fl3DInd,
                    Fl3DBak,
                    Submenu,
                    Flag12,Flag13,Flag14,Flag15);
#endif
     ObjectFlag = PACKEDSET OF ObjectFlags;

CONST None = ObjectFlag{};

(* object states *)

#if revers
TYPE ObjectStates = (State15,State14,State13,Stat12,State11,State10,State9,State8,
                     Draw3D,
                     WhiteBak,
                     Shadowed,
                     Outlined,
                     Disabled,
                     Checked,
                     Crossed,
                     Selected);
#else
TYPE ObjectStates = (Selected,
                     Crossed,
                     Checked,
                     Disabled,
                     Outlined,
                     Shadowed,
                     WhiteBak,
                     Draw3D,
                     State8,State9,State10,State11,State12,State13,State14,State15);
#endif
     ObjectState = PACKEDSET OF ObjectStates;

CONST Normal = ObjectState{};

(* fonts *)

CONST IBM   = 3;
      Small = 5;

(* editable text justifications *)

TYPE TextAlignments = (TeLeft,TeRight,TeCntr);

(* object colors *)

TYPE ObjectColors = (White,
                     Black,
                     Red,
                     Green,
                     Blue,
                     Cyan,
                     Yellow,
                     Magenta,
                     LWhite,
                     LBlack,
                     LRed,
                     LGreen,
                     LBlue,
                     LCyan,
                     LYellow,
                     LMagenta);

CONST DWhite   = LWhite;
      DBlack   = LBlack;
      DRed     = LRed;
      DGreen   = LGreen;
      DBlue    = LBlue;
      DCyan    = LCyan;
      DYellow  = LYellow;
      DMagenta = LMagenta;

(* direct access to TEdInfo^.TEColor and ObjectInfo.Color bit-fields *)

TYPE ObjectField = WORDSET;

CONST PatternShift  = 4;
      PatternOffset = 16;

TYPE InsidePatterns = (IPHollow,
                       IP1Patt,IP2Patt,IP3Patt,IP4Patt,IP5Patt,IP6Patt,
                       IPSolid);

CONST ModeShift  = 7;
      ModeOffset = 128;

TYPE InsideModes = (IPTrans,IPErase);

CONST TextShift  = 8;
      TextOffset = 256;

CONST FrameShift  = 12;
      FrameOffset = 4096;

(* string definition (AES.String, AES.StringPtr) *)

CONST MaxLen = 81;

begin_word
TYPE StringRange = [0..(MaxLen - 1)];
end_word

     String = ARRAY StringRange OF CHAR;

     StringPtr = POINTER TO String;

(* text edit information *)

TYPE TEdInfo = RECORD
                 TEPText    : StringPtr; (* pointer to text *)
                 TEPTmplt   : StringPtr; (* pointer to template *)
                 TEPValid   : StringPtr; (* pointer to validity *)
                 TEFont     : UNSIGNEDWORD; (* IBM or Small *)
                 TEResvd1   : ANYWORD;
#if linear
#if fill_byte
                 FillByte: ANYBYTE;
#endif
#endif
                 TEJust     : TextAlignments;
#if intel
#if fill_byte
                 FillByte: ANYBYTE;
#endif
#endif
                 TEColor    : ObjectField; (* color bit-field *)
                 TEResvd2   : ANYWORD;
                 TEThickness: SIGNEDWORD; (* -128..+127 *)
                 TETxtLen   : StringRange; (* text length *)
                 TETmpLen   : StringRange; (* template length *)
               END;

     TEdInfoPtr = POINTER TO TEdInfo;

(* direct access to IconBlk^.IBColor bit-field *)

TYPE IconColor = BYTESET;

CONST MaskShift = 0;

CONST DataShift = 4;

(* icon structure *)

TYPE IconBlk = RECORD
                 IBPMask: ANYPOINTER; (* pointer to mask *)
                 IBPData: ANYPOINTER; (* pointer to data *)
                 IBPText: StringPtr; (* pointer to text *)
(*
#if intel
                 IBChar : CHAR; (* icon character *)
                 IBColor: IconColor; (* color bit-field *)
#else
                 IBColor: IconColor; (* color bit-field *)
                 IBChar : CHAR; (* icon character *)
#endif
*)
                 IBChar : UNSIGNEDWORD;
                 IBXChar: SIGNEDWORD; (* character position... *)
                 IBYChar: SIGNEDWORD;
                 IBXIcon: SIGNEDWORD; (* icon rectangle... *)
                 IBYIcon: SIGNEDWORD;
                 IBWIcon: UNSIGNEDWORD;
                 IBHIcon: UNSIGNEDWORD;
                 IBXText: SIGNEDWORD; (* text rectangle... *)
                 IBYText: SIGNEDWORD;
                 IBWText: UNSIGNEDWORD;
                 IBHText: UNSIGNEDWORD;
                 IBResvd: ANYWORD;
               END;

     IconBlkPtr = POINTER TO IconBlk;

(* color icon structure *)

TYPE CIconPtr = POINTER TO CIcon;

     CIcon = RECORD
               NumPlanes: UNSIGNEDWORD;
               ColData  : ANYPOINTER;
               ColMask  : ANYPOINTER;
               SelData  : ANYPOINTER;
               SelMask  : ANYPOINTER;
               NextRes  : CIconPtr;
             END;

     CIconBlk = RECORD
                  MonoBlk : IconBlk;
                  MainList: CIconPtr;
                END;

     CIconBlkPtr = POINTER TO CIconBlk;

(* bit image structure *)

TYPE BitBlk = RECORD
                BIPData: ANYPOINTER; (* pointer to data *)
                BIWB   : UNSIGNEDWORD; (* width per bytes *)
                BIHL   : UNSIGNEDWORD; (* height per lines *)
                BIX    : SIGNEDWORD; (* x-position *)
                BIY    : SIGNEDWORD; (* y-position *)
#if linear
#if fill_byte
                FillByte: ANYBYTE;
#endif
#endif
                BIColor: ObjectColors; (* bit image color *)
#if intel
#if fill_byte
                FillByte: ANYBYTE;
#endif
#endif
              END;

     BitBlkPtr = POINTER TO BitBlk;

(* special object information *)

TYPE UserBlkPtr = POINTER TO UserBlk;

     ApplBlkPtr = UserBlkPtr;

     ExtensionPtr = POINTER TO ObjectExtension;

     ObjectSpec = RECORD
                    CASE: ObjectTypes OF
                      GObjc0:
                        Address: ANYPOINTER;
                    | GObjc1:
                        Extension: ExtensionPtr;
                    | GObjc6:
                        HexCode: UNSIGNEDLONG;
                    | GBox,GIBox,GBoxChar:
#if packing
#if linear
                        Char     : CHAR; (* optional character *)
                        Thickness: SIGNEDBYTE; (* -128..+127 *)
                        Color    : ObjectField; (* color bit-field *)
#elif intel
                        Color    : ObjectField; (* color bit-field *)
                        Thickness: SIGNEDBYTE; (* -128..+127 *)
                        Char     : CHAR; (* optional character *)
#endif
#else /* not packing */
                        Color    : UNSIGNEDLONG;
#endif
                    | GText,GFText,GBoxText,GFBoxText:
                        TextInfo: TEdInfoPtr;
                    | GUserDef:
#if no_forward_pointers
                        UserBlock: ANYPOINTER;
#else
                        UserBlock: UserBlkPtr;
#endif
                    | GIcon:
                        IconBlock: IconBlkPtr;
                    | GCIcon:
                        CIconBlock: CIconBlkPtr;
                    | GImage:
                        BitBlock: BitBlkPtr;
                    | GButton,GString,GTitle:
                        String: StringPtr;
                    ELSE
                      UserSpec: ANYPOINTER;
                    END;
                  END;

     ObjectExtension = RECORD
                         Spec: ObjectSpec;
                         Parm: ANYPOINTER;
                       END;

(* AES object and object tree *)

CONST Nil = -1;

TYPE ObjectPtr = [Nil..(MaxObject - 1)];

TYPE Object = RECORD
                ObNext  : ObjectPtr;
                ObHead  : ObjectPtr;
                ObTail  : ObjectPtr;
#if fill_byte
#  if linear
                ObExtnd : UNSIGNEDBYTE;
                ObType  : ObjectTypes;
#  elif intel
                ObType  : ObjectTypes;
                ObExtnd : UNSIGNEDBYTE;
#  endif
#elif packing
#  if linear
                ObExtnd : UNSIGNEDBYTE;
                ObType  : UNSIGNEDBYTE;
#  elif intel
                ObType  : UNSIGNEDBYTE;
                ObExtnd : UNSIGNEDBYTE;
#  endif
#else
                ObType  : UNSIGNEDWORD;
#endif
                ObFlags : ObjectFlag;
                ObState : ObjectState;
                ObSpec  : ObjectSpec;
                ObX     : SIGNEDWORD;
                ObY     : SIGNEDWORD;
                ObWidth : SIGNEDWORD;
                ObHeight: SIGNEDWORD;
              END;

     TreePtr = POINTER TO ARRAY ObjectIndex OF Object;

(* parameter block *)

TYPE UserDefParm = POINTER TO UserDefBlk;

     ParmBlk = RECORD
                 PBTree     : TreePtr;
                 PBObj      : ObjectIndex;
                 PBPrevState: ObjectState;
                 PBCurrState: ObjectState;
                 PBX        : SIGNEDWORD;
                 PBY        : SIGNEDWORD;
                 PBW        : SIGNEDWORD;
                 PBH        : SIGNEDWORD;
                 PBXC       : SIGNEDWORD;
                 PBYC       : SIGNEDWORD;
                 PBWC       : SIGNEDWORD;
                 PBHC       : SIGNEDWORD;
                 PBParm     : UserDefParm;
               END;

#ifdef FSTM2
     UserDefFunc = PROCEDURE(VAR ParmBlk): ANYWORD;
#else
     UserDefFunc = PROCEDURE(VAR ParmBlk): ObjectState;
#endif

#if (defined LPRM2) || (defined SPCM2)
     UserDefCode = PROCEDURE(VAR ParmBlk);
#elif (defined ANAM2) || (defined FTLM2)
     UserDefCode = UserDefFunc;
#elif (defined MM2)
     UserDefCode = ANYPOINTER;
#elif (defined TSM2_1) || (defined TSM2_2)
     UserDefCode = PROC;
#elif (defined FSTM2)
     UserDefCode = PROCEDURE();
#elif (defined HM2) || (defined TDIM2)
     UserDefCode = PROCEDURE(VAR ParmBlk);
#elif (defined LM2)
     UserDefCode = PROC;
#else
     UserDefCode = UserDefFunc;
#endif

     UserDefBlk = RECORD
                    Func: UserDefFunc;
                    Parm: ANYPOINTER;
                  END;

     UserBlk = RECORD
                 UBCode: UserDefCode;
                 UBParm: UserDefParm;
               END;

     ApplBlk = UserBlk;

(* control array *)

TYPE ControlArray = RECORD
                      OpCode     : UNSIGNEDWORD;
                      SizeIntIn  : UNSIGNEDWORD;
                      SizeIntOut : UNSIGNEDWORD;
                      SizeAddrIn : UNSIGNEDWORD;
                      SizeAddrOut: UNSIGNEDWORD;
                    END;

VAR Control: ControlArray;

(* global block *)

CONST MaxTree = 512; (* arbitrary limit *)

TYPE TreeIndex = [0..(MaxTree - 1)];

     TrPtr = POINTER TO ARRAY TreeIndex OF TreePtr;

     RsHdr = RECORD
               RsHVrsn   : UNSIGNEDWORD;
               RsHObject : UNSIGNEDWORD;
               RsHTEdInfo: UNSIGNEDWORD;
               RsHIconBlk: UNSIGNEDWORD;
               RsHBitBlk : UNSIGNEDWORD;
               RsHFrStr  : UNSIGNEDWORD;
               RsHString : UNSIGNEDWORD;
               RsHImData : UNSIGNEDWORD;
               RsHFrImg  : UNSIGNEDWORD;
               RsHTrIndex: UNSIGNEDWORD;
               RsHNObs   : UNSIGNEDWORD;
               RsHNTree  : UNSIGNEDWORD;
               RsHNTEd   : UNSIGNEDWORD;
               RsHNIB    : UNSIGNEDWORD;
               RsHNBB    : UNSIGNEDWORD;
               RsHNString: UNSIGNEDWORD;
               RsHNImages: UNSIGNEDWORD;
               RsHRsSize : UNSIGNEDWORD;
             END;

     RsPtr = POINTER TO RsHdr;

     GlobalBlk = RECORD
                   ApVersion : UNSIGNEDWORD; (* GEM version *)
                   ApCount   : UNSIGNEDWORD; (* number of tasks *)
                   ApId      : UNSIGNEDWORD; (* application identification *)
                   ApPPrivate: ObjectSpec; (* pointer to private things *)
                   ApPTree   : TrPtr; (* pointer to tree array *)
                   ApPMem    : RsPtr; (* pointer to resource memory *)
                   ApLMem    : UNSIGNEDWORD; (* length of resource memory *)
                   ApNPlanes : UNSIGNEDWORD; (* number of planes *)
                   ApResvd   : ANYLONG;
                 (*ApRes     : UNSIGNEDWORD; (* current resolution *)*)
                 (*ApNColors : UNSIGNEDWORD; (* number of colors  *)*)
                   ApBVDisk  : WORDSET; (* bit vector of floppy disk drives *)
                   ApBVHard  : WORDSET; (* bit vector of hard disk drives *)
                 END;

VAR Global: GlobalBlk;

(* integer input and output array *)

CONST MaxInput = 16;
      MaxOutput = 7;

TYPE Integer = SIGNEDWORD;

     IntegerInput = RECORD
                      CASE: UNSIGNEDWORD OF
                        0:
                          Array: ARRAY[0..(MaxInput - 1)] OF Integer;
                      | 1:
                          WH: Integer;
                          WF: Integer;
                          WA: ANYPOINTER;
                          WO: Integer;
                      ELSE
                        Magic: UNSIGNEDLONG;
                      END;
                    END;

     IntegerOutput = ARRAY[0..(MaxOutput - 1)] OF Integer;

VAR IntIn: IntegerInput;
    IntOut: IntegerOutput;

(* address array *)

CONST MaxAddress = 3;

TYPE Address = ANYPOINTER;
     AddressArray = ARRAY[0..(MaxAddress - 1)] OF Address;

VAR Addr: AddressArray;

(* AES parameter block *)

TYPE AESPB = RECORD
               PControl: POINTER TO ControlArray;
               PGlobal : POINTER TO GlobalBlk;
               PIntIn  : POINTER TO IntegerInput;
               PIntOut : POINTER TO IntegerOutput;
               PAdrIn  : POINTER TO AddressArray;
               PAdrOut : POINTER TO Address;
             END;

VAR c: AESPB;

(* AES call and AES utilities (AES.Error(), AES.Version() ) *)

#if Seimet
PROCEDURE crystal;
#else
PROCEDURE crystal(FuncNo,NIntIn,NIntOut,NAddrIn: UNSIGNEDWORD);
#endif

PROCEDURE Error(): BOOLEAN;

PROCEDURE Version(): UNSIGNEDWORD;

#if ST
VAR KAOS: BOOLEAN; (* KAOS flag *)
#endif

END AES.
